﻿using UnityEngine;
using System.Collections;
using System;

public class JohnnyDate : MonoBehaviour {

	public Mesh[] NumberMeshes;

	public MeshFilter MonthTens;
	public MeshFilter MonthOnes;
	public MeshFilter DayTens;
	public MeshFilter DayOnes;
	public MeshFilter YearThousands;
	public MeshFilter YearHundreds;
	public MeshFilter YearTens;
	public MeshFilter YearOnes;

	int LastSeconds;
	DateTime CurrentDT;

	// Use this for initialization
	void Start () {
		LastSeconds = System.DateTime.Now.AddSeconds(-1).Second;
	}
	
	// Update is called once per frame
	void Update () {
		CurrentDT = DateTime.Now;
		if (CurrentDT.Second != LastSeconds) {
			this.MonthTens.mesh = GetNumberMesh(CurrentDT.Month, 2);
			this.MonthOnes.mesh = GetNumberMesh(CurrentDT.Month, 1);
			this.DayTens.mesh = GetNumberMesh(CurrentDT.Day, 2);
			this.DayOnes.mesh = GetNumberMesh(CurrentDT.Day, 1);
			this.YearThousands.mesh = GetNumberMesh(CurrentDT.Year, 4);
			this.YearHundreds.mesh = GetNumberMesh(CurrentDT.Year, 3);
			this.YearTens.mesh = GetNumberMesh(CurrentDT.Year, 2);
			this.YearOnes.mesh = GetNumberMesh(CurrentDT.Year, 1);
			LastSeconds = CurrentDT.Second;
		}

	}

	Mesh GetNumberMesh(int number, int place) {
		int currentNumber = number % (int)Math.Pow(10,place) / ((place == 1) ? 1 : ((int)Math.Pow(10,place-1)));
		//Debug.Log(currentNumber);
		return NumberMeshes[currentNumber];
	}

}
